package cn.itrip.search.service;

import cn.itrip.search.vo.HotelParam;
import cn.itrip.search.vo.ItripHotelVo;
import cn.itrip.utlis.PageBean;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description :
 * Created by Li on 2020-12-24 16:59:34
 */
@Service
public class SearchService {
    public List<ItripHotelVo> searchItripHotelListByHotCity(String cityId, Integer count) throws Exception{
        //连接URL
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient httpSolrClient= new HttpSolrClient (url);
        httpSolrClient.setParser(new XMLResponseParser());//设置相应
        httpSolrClient.setConnectionTimeout(500);//建立连接的最长时间
        SolrQuery solrQuery = new SolrQuery();//新建查询
        solrQuery.setQuery("*:*");
        solrQuery.addFilterQuery("cityId:"+cityId);
        solrQuery.setStart(0);
        solrQuery.setRows(count);
        QueryResponse queryResponse = httpSolrClient.query(solrQuery);
        return queryResponse.getBeans(ItripHotelVo.class);
    }

    public void searchItripHotelPage(HotelParam hotelParam,PageBean pageBean) throws Exception{
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        if(hotelParam.getKeywords() != null) {
            query.addFilterQuery("keyword:"+hotelParam.getKeywords());
        }
        query.setStart((pageBean.getCurPage()-1)*pageBean.getPageSize());
        query.setRows(pageBean.getPageSize());
        QueryResponse response = client.query(query);
        pageBean.setTotal(new Long(response.getResults().getNumFound()).intValue());
        List<ItripHotelVo> list = client.query(query).getBeans(ItripHotelVo.class);
        pageBean.setRows(list);
    }
}
